﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThisIsFirstTest.Filter;
using MealBooking.Core.Models.Accounts;
using MealBooking.Core;
using MealBooking.Core.Models.Dishes;
using ThisIsFirstTest.Models.DishModel;
using MealBooking.Core.Models.Meals;

namespace ThisIsFirstTest.Areas.Manager.Controllers
{
    [AccountAuthorize(AccountRoles = Role.Manager)]
    public class DishManagerController : Controller
    {
        #region Dish
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult AllDishes()
        {
            var dishes = CoreService.DishService.AllDishes();
            return View(dishes);
        }
        public ActionResult DishDetail(string id, string headerMessage = null)
        {
            ViewBag.HeaderTitle = headerMessage;
            var dish = CoreService.DishService.DishDetail(id);
            return View(dish);
        }
        public ActionResult AddNewDish()
        {
            ViewBag.HeaderTitle = "Add New Dish";
            return View();
        }
        [HttpPost]
        public ActionResult AddNewDish(DishModel dishModel)
        {
            if (!ModelState.IsValid)
            {
                return View(dishModel);
            }
            var sameNameDish = CoreService.DishService.IsDishNameInUse(dishModel.Name);
            if (sameNameDish != null)
            {
                ViewBag.SameNameDish = sameNameDish;
                return View(dishModel);
            }
            else
            {
                var dish = Convert(dishModel);
                dish = CoreService.DishService.AddNewDish(dish);
                return RedirectToAction("DishDetail", new { id = dish.Id, headerMessage = "Add New Dish Success. " });
            }
        }
        public ActionResult EditDish(string id)
        {
            ViewBag.HeaderTitle = "Edit Dish Information";
            var dish = CoreService.DishService.DishDetail(id);
            return View(dish);
        }
        [HttpPost]
        public ActionResult EditDish(DishModel dishModel)
        {
            if (!ModelState.IsValid)
            {
                return RedirectToAction("EditDish");
            }
            var sameNameDish = CoreService.DishService.IsDishNameInUse(dishModel.Name);
            if (sameNameDish != null && !sameNameDish.Id.Equals(dishModel.Id))
            {
                ViewBag.SameNameDish = sameNameDish;
                return View(dishModel);
            }
            else
            {
                var dish = Convert(dishModel);
                dish = CoreService.DishService.UpdateDish(dish);
                return RedirectToAction("DishDetail", new { id = dish.Id, headerMessage = "Update Success. " });
            }
        }
        private Dish Convert(DishModel dishModel)
        {
            return new Dish
            {
                Id = dishModel.Id,
                Name = dishModel.Name,
                Price = dishModel.Price,
                Category = dishModel.Category,
                Description = dishModel.Description,
            };
        }
        #endregion

        #region for Dish used Category
        public ActionResult ManyDishesPartial(string id = null)
        {
            var dishes = CoreService.DishService.AllDishes();
            List<Dish> selectedDishes = new List<Dish>();
            List<Dish> dishesWithoutDishCategory = new List<Dish>();
            List<Dish> dishesWithDishCategory = new List<Dish>();
            foreach (var dish in dishes)
            {
                if (dish.Category == null)
                {
                    dishesWithoutDishCategory.Add(dish);
                }
                else if (id != null && id.Equals(dish.Category.Id))
                {
                    selectedDishes.Add(dish);
                }
                else
                {
                    dishesWithDishCategory.Add(dish);
                }
            }
            ViewBag.SelectedDishes = selectedDishes;
            ViewBag.DishesWithoutDishCategory = dishesWithoutDishCategory;
            ViewBag.Dishes = dishesWithDishCategory;
            return PartialView();
        }
        /// <summary>
        /// all dish categories, and selected the one in current dish
        /// </summary>
        /// <param name="dishCategoryId"></param>
        /// <returns></returns>
        public ActionResult SingleDishCategoryPartial(string dishCategoryId = null)
        {
            var dishCategories = CoreService.DishService.AllDishCategories();
            ViewBag.SelectedDishCategoryId = dishCategoryId;
            return PartialView(dishCategories);
        }
        #endregion

        #region Dish Category
        public ActionResult AllDishCategories()
        {
            var dishCategories = CoreService.DishService.AllDishCategories();
            return View(dishCategories);
        }
        public ActionResult DishCategoryDetail(string id, string headerMessage = null)
        {
            if (headerMessage != null)
            {
                ViewBag.HeaderTitle = headerMessage;
            }
            else
            {
                ViewBag.HeaderTitle = "Dish Category Detail";
            }
            var dishCategory = CoreService.DishService.DishCategoryDetail(id);
            return View(dishCategory);
        }
        public ActionResult AddNewDishCategory()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddNewDishCategory(DishCategoryModel newDishCategory)
        {
            if (!ModelState.IsValid)
            {
                return View(newDishCategory);
            }
            var sameNameDishCategory = CoreService.DishService.IsDishCategoryNameInUse(newDishCategory.Name);
            if (sameNameDishCategory != null)
            {
                ViewBag.SameNameDishCategory = sameNameDishCategory;
                return View(newDishCategory);
            }
            var dishCategory = Convert(newDishCategory);
            dishCategory = CoreService.DishService.AddDishCategory(dishCategory);
            return RedirectToAction("DishCategoryDetail", new { id = dishCategory.Id, headerMessage = "Add New Dish Category Success. " });
        }
        public ActionResult EditDishCategory(string id)
        {
            ViewBag.HeaderTitle = "Edit Dish Category Information";
            var dishCategory = CoreService.DishService.DishCategoryDetail(id);
            ViewBag.SelectedDishes = dishCategory.Dishes;
            return View(dishCategory);
        }
        [HttpPost]
        public ActionResult EditDishCategory(DishCategoryModel dishCategoryModel)
        {
            if (!ModelState.IsValid)
            {
                return View(dishCategoryModel);
            }
            var sameNameDishCategory = CoreService.DishService.IsDishCategoryNameInUse(dishCategoryModel.Name);
            if (sameNameDishCategory != null && !dishCategoryModel.Id.Equals(sameNameDishCategory.Id))
            {
                ViewBag.SameNameDishCategory = sameNameDishCategory;
                return View(dishCategoryModel);
            }
            var dishCategory = Convert(dishCategoryModel);
            dishCategory = CoreService.DishService.UpdateDishCategory(dishCategory);
            return RedirectToAction("DishCategoryDetail", new { id = dishCategory.Id, headerMessage = "Update Success. " });
        }
        private DishCategory Convert(DishCategoryModel newDishCategory)
        {
            var dishCategory = new DishCategory
            {
                Id = newDishCategory.Id,
                Name = newDishCategory.Name,
                Category = newDishCategory.Category,
                Description = newDishCategory.Description
            };
            if (newDishCategory.DishIds != null && newDishCategory.DishIds.Count != 0)
            {
                dishCategory.Dishes = newDishCategory.DishIds.Select(i => new Dish { Id = i }).ToList();
            }
            return dishCategory;
        }
        #endregion

        #region for DishCategory used MealCategory
        public ActionResult SingleMealCategoryPartial(string mealCategoryId = null)
        {
            var mealCategories = CoreService.MealService.AllMealCategories();
            ViewBag.MealCategories = mealCategories;
            if (mealCategoryId != null && mealCategories != null && mealCategories.Count != 0)
            {
                var mealCategory = mealCategories.Single(c => mealCategoryId.Equals(c.Id));
                ViewBag.MealCategory = mealCategory;
            }
            return PartialView();
        }
        #endregion
    }
}
